<style scoped>
.header {
  width: 100%;
  height: 150px;
  background-image: url("http://39.96.21.48/2020_znn/visual-ui/static/img/bg1.efce26ef.png");
  background-size: cover;
  border-radius: 20px 20px 0 0;
  margin: -0.5em;
  margin-bottom: -0.5em;
  margin-bottom: 1em;
  padding: 1em;
  color: #fff;
  position: absolute;
  top: 0;
  left: 0;
}
.header_title {
  line-height: 2em;
  font-size: 20px;
  font-weight: 700;
  margin-left: 10px;
  margin-top: 15px;
}
.icon {
  width: 17px;
  height: 17px;
}
.content {
  margin-top: 160px;
}
.item {
  padding: 0 20px;
  margin-top: 20px;
  width: 100%;
  height: 90px;
  border-bottom: 1px solid #f8f8f8;
}

.left {
  float: left;
}

.left div {
  height: 30px;
  line-height: 30px;
}

.right {
  height: 90px;
  float: right;
}
.right_distance {
  width: 150px;
  line-height: 30px;
  text-align: center;
}
.right_logo {
  text-align: center;
  width: 150px;
  height: 60px;
  line-height: 60px;
}
.right_logo i {
  color: #0074ff;
  font-size: 40px;
}
.content .left div:nth-child(2) {
  background-color: #ffb34e;
  border-radius: 5px;
  display: inline;
  color: #fff;
  padding: 5px;
  font-size: 14px;
}
.content .left div:nth-child(3) {
  color: #999;
  font-size: 14px;
}
</style>
<template>
  <div>
    <div class="header">
      <div class="header_title">
        <i class="iconfont icon-user2"></i>
        广西壮族自治区
      </div>
      <div class="header_title">医疗救治定点医院和发热门诊一览</div>
    </div>
    <!-- {{hos}} -->
    <div class="content">
      <div style="width: 200px">
        <el-input
          size="small"
          v-model="obj.name"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <div
        @click="toMap(item)"
        class="item"
        v-for="item in hos.list"
        :key="item.id"
      >
        <div class="left">
          <div>{{ item.name }}</div>
          <div v-if="item.flags">{{ item.flags }}</div>
          <div v-else>暂无标记</div>
          <div v-if="item.address">{{ item.address }}</div>
          <div v-else>请询问当地人...</div>
        </div>
        <div class="right">
          <!-- icon-dingwei -->
          <div class="right_logo">
            <i class="iconfont icon-location"></i>
          </div>
          <div class="right_distance">
            {{ GetDistance(item.latitude, item.longitude) }}km
          </div>
        </div>
      </div>
      <el-pagination
        :pager-count="11"
        layout="prev, pager, next"
        :total="hos.total"
        :page-size="obj.pageSize"
        :current-page="obj.page"
        @current-change="change_page"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true,
    },
  },
  data() {
    return {
      obj: {
        page: 1,
        pageSize: 10,
      },
      //当前纬度
      lat: 0,
      //当前经度
      lng: 0,
      total: 0,
    };
  },
  computed: {
    ...mapState("hospital", ["hos"]),
  },
  created() {
    this.findHos(this.obj);
    this.getLocation();
  },
  methods: {
    ...mapActions("hospital", ["findHos"]),
    //获取当前位置
    getLocation() {
      navigator.geolocation.getCurrentPosition(
        (res) => {
          console.log(res, "√");
          //当前经度
          this.lng = res.coords.longitude;
          //当前纬度
          this.lat = res.coords.latitude;
        },
        (err) => {
          console.log(err, "×");
        },
        {
          enableHighAccuracy: true /*高精度定位*/,
          timeout: 5000,
          maximumAge: 0,
        }
      );
    },
    change_page(val) {
      //页数同步操作
      this.obj.page = val;
      this.total;
      // console.log(val)
    },

    // 方法定义 lat,lng
    GetDistance(lat1, lng1) {
      var radLat1 = (lat1 * Math.PI) / 180.0;
      var radLat2 = (this.lat * Math.PI) / 180.0;
      var a = radLat1 - radLat2;
      var b = (lng1 * Math.PI) / 180.0 - (this.lng * Math.PI) / 180.0;
      var s =
        2 *
        Math.asin(
          Math.sqrt(
            Math.pow(Math.sin(a / 2), 2) +
              Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)
          )
        );
      s = s * 6378.137; // EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000;
      return s;
    },
    // 调用 return的距离单位为km
    // GetDistance(10.0,113.0,12.0,114.0)
    // 跳转至定点地图中，并将获取到的经纬度传输到定点地图中
    toMap(val) {
      // console.log(val.latitude);
      // console.log(val.longitude);
      this.$router.push({
        //这里是根据路由中的router的固定的地址来跳转
        path: "/hos/detail",
        query: {
          latitude: val.latitude,
          longitude: val.longitude,
        },
      });
    },
  },
};
</script>